package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.IntChange;
import com.wanlian.entity.IntChangeDetail;
import com.wanlian.entity.User;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.IntDetailService;
import com.wanlian.service.TaskTimerServices;
import com.wanlian.service.UserScoreServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private UserScoreServices userScoreServices;

    @Autowired
    private IntDetailService intDetailService;

    @Autowired
    private TaskTimerServices taskTimerService;

    /**
     * 运营平台获取用户积分
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String republish(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        int uid = 0;
        ResultModel resultModel = userScoreServices.queryUserScoreByParam(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台获取用户积分
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/user/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String republishApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.queryUserScoreByParam(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台查询用户积分历史
     * 运营平台可根据用户id查询积分
     */
    @RequestMapping(value = "/user/scorehistory", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String userScoreHistory(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        int uid = 0;
        ResultModel resultModel = userScoreServices.queryUserScoreHistoryByParam(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App查询用户积分历史
     */
    @RequestMapping(value = "/app/user/scorehistory", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String userscorehistoryApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.queryUserScoreHistoryByParam(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 运营平台获取昨日积分
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/score/yes", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreYes(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userScoreServices.queryUserScoreYes(user.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台获取昨日积分
     *
     * @return
     */
    @RequestMapping(value = "/app/user/score/yes", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreYesApp(HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.queryUserScoreYes(uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台获取昨日积分明细
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/score/item", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreYesItem(@RequestBody String data) {
        PageCondition condition = JsonStringUtil.readValue(data, PageCondition.class);
        int userId = (int) condition.getByKey("userId");
        ResultModel resultModel = userScoreServices.queryUserScoreYesItem(condition.getPage(), userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台获取积分明细
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/user/score/item", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreYesItemApp(@RequestBody String data, HttpSession session) {
        PageCondition condition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.queryUserScoreYesItem(condition.getPage(), uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台获取积分流水
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/scorewater", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreWater(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        int uid = 0;
        ResultModel resultModel = userScoreServices.queryUserScoreWater(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App品台获取积分流水
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/user/scorewater", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreWaterApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.queryUserScoreWater(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App品台获取积分排行
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/score/order", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScoreOrder(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = userScoreServices.queryUserScoreOrder(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App品台获取总积分排行
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/sumscore/order", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserSumScoreOrder(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = userScoreServices.queryUserScoreOrder(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 提取int
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/score/changeint", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String changeInt(@RequestBody String data, HttpSession session) {
        IntChange intChange = JsonStringUtil.readValue(data, IntChange.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = userScoreServices.doUpdateInt(intChange, userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * app获取提取int详情
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/score/intdetail", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String intdetail(@RequestBody String data, HttpSession session) {
        IntChangeDetail intChangeDetail = JsonStringUtil.readValue(data, IntChangeDetail.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        ResultModel resultModel = intDetailService.queryIntDetail(intChangeDetail.getShid());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * web获取提取int详情
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score/intdetail", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String intdetailweb(@RequestBody String data, HttpSession session) {
        IntChangeDetail intChangeDetail = JsonStringUtil.readValue(data, IntChangeDetail.class);
        ResultModel resultModel = intDetailService.queryIntDetail(intChangeDetail.getShid());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 积分规则计算类
     */
   /* @RequestMapping(value = "/task/cal", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String testrole() {
        ResultModel resultModel = new ResultModel();
        taskTimerService.doEdit();
        return new JsonResult().Serailze(resultModel);
    }*/
}
